
#include "IEC101ProtocalDefineV1.h"
#include "IEC101ComuStatus.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
extern uint16_t m_nRECWaveAddr;
DeviceState_Type deviceState;

/*ʵʱ¼��λ��ָʾ*/
//ADCHANNELS_TypeDef *ptrBlockStartPOs=NULL;
//ADCHANNELS_TypeDef *ptrCurrPos=NULL;
POSofCURRDataBLOCK currPOs;

RemoteMeasureData_Type remoteMeasure;

DataPack_Type          m_pSendDataFrame;
uint8_t                g_nRemoteSignalSendedIndex;
SendData_Type          g_pRemoteSignalSendAll;
SendData_Type          g_pRemoteMeasureAll;
DataPack_Type          g_RemoteMeasureFrame;
DataPack_Type          g_RemoteSignalFrame;

/*¼��֡��¼��֡����*/

SendData_Type          g_pRecordWaveFrameList;
DataPack_Type          g_RecordWaveFrame;

TimeStruct             timeSpanHeartBeat;

unsigned char CacuCheckCS(unsigned char* pucStart,int nSize)
{
	unsigned int uSum = 0;
	for(int i=0;i<nSize;i++)
	{
		uSum = uSum + pucStart[i];
	}
	unsigned ucCheckCS = uSum%256;
	return ucCheckCS;
}
/***********************************************
*�������ܣ������ն˵��豸״̬���ң��
*
*
*
************************************************/

void MakePackForRemoteSignal(DataPack_Type *m_pSendFrame, DeviceState_Type m_dState,uint16_t m_ASDUAdress)
{
	uint8_t index = 0;
	uint8_t pASPUStart = sizeof(FrameHead_Type);
	
	//���ͷ��
	m_pSendFrame->ucData[0] = 0x68;
	m_pSendFrame->ucData[1] = NUMYX_MAX + 11;//��ַ�򡢿�����ASDU���ܳ���
	m_pSendFrame->ucData[2] = NUMYX_MAX + 11;
	m_pSendFrame->ucData[3] = 0x68;
	m_pSendFrame->ucData[4] = 0;//��������ʱΪ0�����滹Ҫ�޸�
	m_pSendFrame->ucData[5] = m_ASDUAdress;//��ַ��ĵ��ֽ�
	m_pSendFrame->ucData[6] = m_ASDUAdress >> 8;//��ַ��ĸ��ֽ�
	
	m_pSendFrame->ucData[pASPUStart++] = eTI_M_SP_NA_1_SingleInfo; //���ͱ�ʶ������ʱ��ĵ�����Ϣ
	m_pSendFrame->ucData[pASPUStart++] = NUMYX_MAX | 0x80;//�ɱ�ṹ�޶���SQ=1
	m_pSendFrame->ucData[pASPUStart++] =  eCOT_introgen;//����ԭ��ĵͣ���Ӧվ�ٻ�
    m_pSendFrame->ucData[pASPUStart++] = 0;//	����ԭ��ĵͣ���Ӧվ�ٻ�
	m_pSendFrame->ucData[pASPUStart++] = m_ASDUAdress; //ASDU������ַ���ֽ�
	m_pSendFrame->ucData[pASPUStart++] = m_ASDUAdress >> 8;//ASDU������ַ���ֽ�
	m_pSendFrame->ucData[pASPUStart++] = (ADDRESS_YX_BEAS) & 0x00ff;//��Ϣ�����ַ�ĵ��ֽ�
	m_pSendFrame->ucData[pASPUStart++] = ((ADDRESS_YX_BEAS) & 0xff00) >> 8;//��Ϣ�����ַ�ĸ��ֽ�
	for(index = 0; index < NUMYX_MAX ; index++)
	{		
		m_pSendFrame->ucData[pASPUStart++] = (m_dState.w >> index) & 0x01;
	}
	//У��ͺ�0x16
    m_pSendFrame->nSize = NUMYX_MAX + 17;
    m_pSendFrame->ucData[m_pSendFrame->nSize-1] = 0x16;	
 
}

void MakePackForRemoteMeasure(DataPack_Type *m_pSendFrame,RemoteMeasureData_Type *m_pRemoteMeasure, uint16_t m_ASDUAdress)
{
	uint8_t index = 0;
	uint8_t pASDUStart = sizeof(FrameHead_Type);
	//���ͷ��
	m_pSendFrame->ucData[0] = 0x68;
	m_pSendFrame->ucData[1] = NUMYC_MAX*4 +  11;
	m_pSendFrame->ucData[2] = NUMYC_MAX*4 +  11;
	m_pSendFrame->ucData[3] = 0x68;
	m_pSendFrame->ucData[4] = 0;//��������ʱΪ0�����滹Ҫ�޸�
	m_pSendFrame->ucData[5] = m_ASDUAdress;//��ַ��ĵ��ֽ�
	m_pSendFrame->ucData[6] = m_ASDUAdress >> 8;//��ַ��ĸ��ֽ�
	m_pSendFrame->ucData[pASDUStart++] = eTI_M_ME_NC_1_Measure_ShortFloatValue;
	m_pSendFrame->ucData[pASDUStart++] = NUMYC_MAX | 0x80;//�ɱ�ṹ�޶���SQ=1
	m_pSendFrame->ucData[pASDUStart++] =  eCOT_introgen;//����ԭ��ĵͣ���Ӧվ�ٻ�
	m_pSendFrame->ucData[pASDUStart++] = 0;//	����ԭ��ĵͣ���Ӧվ�ٻ�
	m_pSendFrame->ucData[pASDUStart++] = m_ASDUAdress; //ASDU������ַ���ֽ�
	m_pSendFrame->ucData[pASDUStart++] = m_ASDUAdress >> 8;//ASDU������ַ���ֽ�
	m_pSendFrame->ucData[pASDUStart++] = ( ADDRESSYC_BEAS) & 0x00ff;//��Ϣ�����ַ�ĵ��ֽ�
	m_pSendFrame->ucData[pASDUStart++] = (( ADDRESSYC_BEAS) & 0xff00) >> 8;//��Ϣ�����ַ�ĸ��ֽ�
	for(index = 0; index < NUMYC_MAX ; index++)
	{		
		m_pSendFrame->ucData[pASDUStart++] = m_pRemoteMeasure->data[index]&0x000000ff;
		m_pSendFrame->ucData[pASDUStart++] = (m_pRemoteMeasure->data[index]&0x0000ff00)>>8;
		m_pSendFrame->ucData[pASDUStart++] = (m_pRemoteMeasure->data[index]&0x00ff0000)>>16;
		m_pSendFrame->ucData[pASDUStart++] = (m_pRemoteMeasure->data[index]&0xff000000)>>24;
	}
	//�ټ���У��������������17��
	m_pSendFrame->nSize = NUMYC_MAX*4  + 17;	
	m_pSendFrame->ucData[m_pSendFrame->nSize-1] = 0x16;	

}


/*****************************************
�������ܣ����ݸ����Ŀ�����͵�ַ���ֵ�����ɹ̶�֡
�������ƣ�MakeFixFrame
����������_ucLinkControl  �����루1���ֽڣ�
          _u16LinkAddress ��ַ��2���ֽڣ�
����ֵ��ָ��FIXEDFRAME��ָ��
ע�⣺���ù̶�֡����Ҫʱ����Ҫʹ��rt_free�ͷŵ��������ڴ�й©��


******************************************/
void MakeFixFrame(FIXEDFRAME* fixFrame, unsigned char  _ucLinkControl,uint16_t  _u16LinkAddress)
{
	fixFrame->ucStartChar               = 0x10;
	fixFrame->ucLinkControl             = _ucLinkControl;
	fixFrame->u16LinkAddress            = _u16LinkAddress;
	unsigned char* pucAddress = (unsigned char*)&_u16LinkAddress;
	fixFrame->ucCheckCode				  = (fixFrame->ucLinkControl + pucAddress[0]+pucAddress[1])%256;
	fixFrame->ucEndChar                 = 0x16;
}

/*****************************************
�������ܣ�������·������
�������ƣ�MakeFixFrame
����������_ucLinkControl  �����루1���ֽڣ�
          _u16LinkAddress ��ַ��2���ֽڣ�
����ֵ��ָ��FIXEDFRAME��ָ��
ע�⣺���ù̶�֡����Ҫʱ����Ҫʹ��rt_free�ͷŵ��������ڴ�й©��


******************************************/
void ParseLinkControl(unsigned ucLinkControl,LinkControlContent* LCC)
{
	LCC->bDIR = ucLinkControl&0x80  ? TRUE:FALSE;
	LCC->bPRM = ucLinkControl&0x40  ? TRUE:FALSE;
	LCC->bFCB = ucLinkControl&0x20  ? TRUE:FALSE;
	LCC->bFCV = ucLinkControl&0x10  ? TRUE:FALSE;
	LCC->ucFC = ucLinkControl&0x0F;
}





void MakePackForGeneralInterrogation(CMD_GeneralInterrogation* callOn, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT)
{
		callOn->FVH.ucStartChar1   = 0x68;
		callOn->FVH.ucLen1         = sizeof(CMD_GeneralInterrogation) - 6;
		callOn->FVH.ucLen2         = sizeof(CMD_GeneralInterrogation) - 6;
		callOn->FVH.ucStartChar2   = 0x68;
		callOn->FVH.ucLinkControl  = ucLinkControl;
		callOn->FVH.u16LinkAddress = u16LinkAddress;
		callOn->FVH.ucTI           = eTI_C_IC_NA_1_MainStationCallOnCommand;
		callOn->FVH.ucVSQ          = 0x01;
		callOn->FVH.u16COT         = eCOT;
		callOn->FVH.u16ASDUAddr    = u16LinkAddress; //��·��ַ
		callOn->u16InfoObjAddr    = 0x0000;  //��Ϊ0
		callOn->ucQOI    = 20; //���ٻ�
		callOn->ucCS         = CacuCheckCS(&(callOn->FVH.ucLinkControl),callOn->FVH.ucLen1);
		callOn->ucEndChar          = 0x16;
}

void MakePackForTimeSyn(CMD_TimeSyn* timeSyn, unsigned char ucLinkControl,uint16_t  u16LinkAddress,enumCOT eCOT,CP56Time2a __st)
{
		timeSyn->FVH.ucStartChar1   = 0x68;
		timeSyn->FVH.ucLen1         = sizeof(CMD_TimeSyn) - 6;
		timeSyn->FVH.ucLen2         = sizeof(CMD_TimeSyn) - 6;
		timeSyn->FVH.ucStartChar2   = 0x68;
		timeSyn->FVH.ucLinkControl  = ucLinkControl;
		timeSyn->FVH.u16LinkAddress = u16LinkAddress;
		timeSyn->FVH.ucTI           = eTI_C_CS_NA_1_ClockSynCommand;
		timeSyn->FVH.ucVSQ          = 0x01;
		timeSyn->FVH.u16COT         = eCOT;
		timeSyn->FVH.u16ASDUAddr    = u16LinkAddress; //��·��ַ
		timeSyn->u16InfoObjAddr    = 0x0000;  //��Ϊ0
		timeSyn->st            = __st;  //ʱ��
		timeSyn->ucCS         = CacuCheckCS(&(timeSyn->FVH.ucLinkControl),timeSyn->FVH.ucLen1);
		timeSyn->ucEndChar          = 0x16;
}

void MakePackForDelayedSample(CMD_DelayedSample* delayedSample, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT,CP16Time2a __st)
{
		delayedSample->FVH.ucStartChar1   = 0x68;
		delayedSample->FVH.ucLen1         = sizeof(CMD_DelayedSample) - 6;
		delayedSample->FVH.ucLen2         = sizeof(CMD_DelayedSample) - 6;
		delayedSample->FVH.ucStartChar2   = 0x68;
		delayedSample->FVH.ucLinkControl  = ucLinkControl;
		delayedSample->FVH.u16LinkAddress = u16LinkAddress;
		delayedSample->FVH.ucTI           = eTI_C_CD_NA_1_DelayedSampleCommand ;
		delayedSample->FVH.ucVSQ          = 0x01;
		delayedSample->FVH.u16COT         = eCOT;
		delayedSample->FVH.u16ASDUAddr    = u16LinkAddress; //??????????????
		delayedSample->u16InfoObjAddr     = 0x0000;    //??0
		delayedSample->st             = __st;  //??
		delayedSample->ucCS         = CacuCheckCS(&(delayedSample->FVH.ucLinkControl),delayedSample->FVH.ucLen1);
		delayedSample->ucEndChar          = 0x16;
}
void MakePackForResetProcess(CMD_ResetProcess* cmdResetProcess, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT)
{
		cmdResetProcess->FVH.ucStartChar1		= 0x68;
		cmdResetProcess->FVH.ucLen1				= sizeof(CMD_ResetProcess) - 6;
		cmdResetProcess->FVH.ucLen2				= sizeof(CMD_ResetProcess) - 6;
		cmdResetProcess->FVH.ucStartChar2		= 0x68;
		cmdResetProcess->FVH.ucLinkControl		= ucLinkControl;
		cmdResetProcess->FVH.u16LinkAddress		= u16LinkAddress;
		cmdResetProcess->FVH.ucTI				= eTI_C_RP_NA_1_ResetProcessCommand;
		cmdResetProcess->FVH.ucVSQ				= 0x01;
		cmdResetProcess->FVH.u16COT				= eCOT;
		cmdResetProcess->FVH.u16ASDUAddr			= u16LinkAddress; //??????????????
		cmdResetProcess->u16InfoObjAddr			= 0x0000;  //??0
		cmdResetProcess->ucResetProcessQRP = 1; //??????
		cmdResetProcess->ucCS				= CacuCheckCS(&(cmdResetProcess->FVH.ucLinkControl),cmdResetProcess->FVH.ucLen1);
		cmdResetProcess->ucEndChar				= 0x16;
}

void MakePackForInitializedOver(CMD_InitializedOver_Type *cmdInitOver, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOI_InitializeCause eCOI)
{
		cmdInitOver->frame.FVH.ucStartChar1		= 0x68;
		cmdInitOver->frame.FVH.ucLen1				= sizeof(CMD_InitializedOver_Type) - 6;
		cmdInitOver->frame.FVH.ucLen2				= sizeof(CMD_InitializedOver_Type) - 6;
		cmdInitOver->frame.FVH.ucStartChar2		= 0x68;
		cmdInitOver->frame.FVH.ucLinkControl		= ucLinkControl;
		cmdInitOver->frame.FVH.u16LinkAddress		= u16LinkAddress;
		cmdInitOver->frame.FVH.ucTI				= eTI_M_EI_NA_1_InitComplete;
		cmdInitOver->frame.FVH.ucVSQ				= 0x01;
		cmdInitOver->frame.FVH.u16COT				= eCOT_init;
		cmdInitOver->frame.FVH.u16ASDUAddr			= u16LinkAddress; //??????????????
		cmdInitOver->frame.u16InfoObjAddr			= 0x0000;  //??0
		cmdInitOver->frame.ucCOI         = eCOI;    //?????
		cmdInitOver->frame.ucCS				= CacuCheckCS(&cmdInitOver->frame.FVH.ucLinkControl,cmdInitOver->frame.FVH.ucLen1);
		cmdInitOver->frame.ucEndChar				= 0x16;
}

void MakePackForTestCommand(CMD_TestCommand *cmdTest, unsigned char ucLinkControl,uint16_t u16LinkAddress,enumCOT eCOT)
	{
		cmdTest->FVH.ucStartChar1		= 0x68;
		cmdTest->FVH.ucLen1				  = sizeof(CMD_TestCommand) - 6;
		cmdTest->FVH.ucLen2				  = sizeof(CMD_TestCommand) - 6;
		cmdTest->FVH.ucStartChar2		= 0x68;
		cmdTest->FVH.ucLinkControl		= ucLinkControl;
		cmdTest->FVH.u16LinkAddress		= u16LinkAddress;
		cmdTest->FVH.ucTI				      = eTI_C_TS_NA_1_TestCommand;
		cmdTest->FVH.ucVSQ				    = 0x01;
		cmdTest->FVH.u16COT				    = eCOT;
		cmdTest->FVH.u16ASDUAddr			= u16LinkAddress; //??????????????
		cmdTest->u16InfoObjAddr			  = 0x0000;  //??0
		cmdTest->u16FBP               = 0x55AA;  //???
		cmdTest->ucCS				          = CacuCheckCS(&cmdTest->FVH.ucLinkControl,cmdTest->FVH.ucLen1);
		cmdTest->ucEndChar				    = 0x16;
	}
	
	
void MakePackForCMD_AbnormityReport(CMD_AbnormityReport* CMD_Abnormity, unsigned char ucLinkControl, uint16_t u16LinkAddress, TimeStruct *tm, enumCOT eCOT)
{
	  CMD_Abnormity->FVH.ucStartChar1          = 0x68;
		CMD_Abnormity->FVH.ucLen1                = sizeof(CMD_AbnormityReport) - 6;
		CMD_Abnormity->FVH.ucLen2                = sizeof(CMD_AbnormityReport) - 6;
		CMD_Abnormity->FVH.ucStartChar2          = 0x68;
		CMD_Abnormity->FVH.ucLinkControl         = ucLinkControl;
		CMD_Abnormity->FVH.u16LinkAddress        = u16LinkAddress;
		CMD_Abnormity->FVH.ucTI                  = eTI_M_FT_NA_1_FaultEventInfo;
		CMD_Abnormity->FVH.ucVSQ                 = 0x01;
		CMD_Abnormity->FVH.u16COT                = eCOT;
		CMD_Abnormity->FVH.u16ASDUAddr           = u16LinkAddress; //�ڹ���ָʾ���ڲ�������·��ַ
		CMD_Abnormity->u16InfoObjAddr            = 0x0000;  //��Ϊ0		
		CMD_Abnormity->cp56tFileName             = MakeCP56Time2a(*tm);

		CMD_Abnormity->u16REServed1 = 0;
		CMD_Abnormity->u16REServed2 = 0;
		CMD_Abnormity->u16REServed3 = 0;
		CMD_Abnormity->ucQoR        = 80;
		CMD_Abnormity->ucCheckCode  = CacuCheckCS(&(CMD_Abnormity->FVH.ucLinkControl), CMD_Abnormity->FVH.ucLen1);
		CMD_Abnormity->ucEndChar    = 0x16;
	
}

void MakePackForCMD_FaultReport(CMD_AbnormityReport* CMD_Abnormity, unsigned char ucLinkControl, uint16_t u16LinkAddress, CP56Time2a *tm, enumCOT eCOT)
{
	  CMD_Abnormity->FVH.ucStartChar1          = 0x68;
		CMD_Abnormity->FVH.ucLen1                = sizeof(CMD_AbnormityReport) - 6;
		CMD_Abnormity->FVH.ucLen2                = sizeof(CMD_AbnormityReport) - 6;
		CMD_Abnormity->FVH.ucStartChar2          = 0x68;
		CMD_Abnormity->FVH.ucLinkControl         = ucLinkControl;
		CMD_Abnormity->FVH.u16LinkAddress        = u16LinkAddress;
		CMD_Abnormity->FVH.ucTI                  = eTI_M_FT_NA_1_FaultEventInfo;
		CMD_Abnormity->FVH.ucVSQ                 = 0x01;
		CMD_Abnormity->FVH.u16COT                = eCOT;
		CMD_Abnormity->FVH.u16ASDUAddr           = u16LinkAddress; //�ڹ���ָʾ���ڲ�������·��ַ
		CMD_Abnormity->u16InfoObjAddr            = 0x0000;  //��Ϊ0
    //memcmp(&CMD_Abnormity->cp56tFileName,tm,sizeof(CP56Time2a));
		CMD_Abnormity->cp56tFileName             = MakeCP56Time2aFromCP56(tm);

		CMD_Abnormity->u16REServed1 = 0;
		CMD_Abnormity->u16REServed2 = 0;
		CMD_Abnormity->u16REServed3 = 0;
		CMD_Abnormity->ucQoR        = 80;
		CMD_Abnormity->ucCheckCode  = CacuCheckCS(&(CMD_Abnormity->FVH.ucLinkControl), CMD_Abnormity->FVH.ucLen1);
		CMD_Abnormity->ucEndChar    = 0x16;
	
}

void MakePackForCMD_SoftwareUpdateStartOrStopConfirm(CMD_SoftwareUpdateConfirm* CMD_UpdateConfirm, uint8_t ucLinkControl, uint16_t u16LinkAddress, enumCOT eCOT, bool start)
{
	CMD_UpdateConfirm->FVH.ucStartChar1 = 0x68;
	CMD_UpdateConfirm->FVH.ucLen1 = sizeof(CMD_SoftwareUpdateConfirm) - 6;
	CMD_UpdateConfirm->FVH.ucLen2 = sizeof(CMD_SoftwareUpdateConfirm) - 6;
	CMD_UpdateConfirm->FVH.ucStartChar2 = 0x68;
	CMD_UpdateConfirm->FVH.ucLinkControl = ucLinkControl;
	CMD_UpdateConfirm->FVH.u16LinkAddress = u16LinkAddress;
	CMD_UpdateConfirm->FVH.ucTI = eTI_F_SR_NA_1_SoftwareUpdate;
	CMD_UpdateConfirm->FVH.ucVSQ           = 0x01;
	CMD_UpdateConfirm->FVH.u16COT          = eCOT;
	CMD_UpdateConfirm->FVH.u16ASDUAddr     = u16LinkAddress;
	CMD_UpdateConfirm->u16InfoObjAddr      = 0x0000;
	if(TRUE == start)
		CMD_UpdateConfirm->ucCtype = 0x80;
	else if(FALSE == start)
		CMD_UpdateConfirm->ucCtype = 0x00;
	CMD_UpdateConfirm->ucCheckCode         = CacuCheckCS(&(CMD_UpdateConfirm->FVH.ucLinkControl), CMD_UpdateConfirm->FVH.ucLen1);
	CMD_UpdateConfirm->ucEndChar           = 0x16;
}

void MakePackForCMD_RealRecWave(CMD_RealRecWave* CMD_RealRecWav, uint8_t ucLinkControl, uint16_t u16LinkAddress, TimeStruct *tm, enumCOT eCOT)
{
	  CMD_RealRecWav->FVH.ucStartChar1    = 0x68;
		CMD_RealRecWav->FVH.ucLen1          = sizeof(CMD_RealRecWave) - 6;
		CMD_RealRecWav->FVH.ucLen2          = sizeof(CMD_RealRecWave) - 6;
		CMD_RealRecWav->FVH.ucStartChar2    = 0x68;
		CMD_RealRecWav->FVH.ucLinkControl   = ucLinkControl;
		CMD_RealRecWav->FVH.u16LinkAddress  = u16LinkAddress;
		CMD_RealRecWav->FVH.ucTI            = eTI_F_RE_WA_1_CallRecordData;
		CMD_RealRecWav->FVH.ucVSQ           = 0x01;
		CMD_RealRecWav->FVH.u16COT          = eCOT;
		CMD_RealRecWav->FVH.u16ASDUAddr     = u16LinkAddress; //??????????????
		CMD_RealRecWav->u16InfoObjAddr      = 0x0000;  //??0
		CMD_RealRecWav->ucOPerationID       = 3;
		CMD_RealRecWav->ucFileNameLen       = 7;
		//COleDateTime tm= COleDateTime::GetCurrentTime();
		CMD_RealRecWav->cp56tFileName       =  MakeCP56Time2a(*tm);
		CMD_RealRecWav->ucCheckCode         = CacuCheckCS(&(CMD_RealRecWav->FVH.ucLinkControl), CMD_RealRecWav->FVH.ucLen1);
		CMD_RealRecWav->ucEndChar           = 0x16;
	
}
/*��װ¼������֡*/
void MakePackForRecordData(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint8_t byteOfDataSizeInPack)
{

		    /*ȷ���ײ���Ϣ������*/
		RECORD_WAVE_HEAD* pFrameVar = (RECORD_WAVE_HEAD*)pSendFrame->ucData;
		pFrameVar->FVH.ucStartChar1 = 0x68;
		pFrameVar->FVH.ucLen1       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		pFrameVar->FVH.ucLen2       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		   /*�Ƿ������һ֡����������־*/
	  if (isLastDataFrame)
		{
			pFrameVar->ucEndFlag  = 1;
		}
		else{
			pFrameVar->ucEndFlag  = 0;
		}

		pFrameVar->FVH.ucStartChar2   = 0x68;		                  
		unsigned char ucLinkControl   = MakeLinkControl(TRUE, TRUE, TRUE, TRUE, eFC_Send_Confirm_UserData);//���������  
		pFrameVar->FVH.ucLinkControl  = ucLinkControl;
		pFrameVar->FVH.u16LinkAddress = u16LinkAddress;		           //��·��ַ		
		pFrameVar->FVH.ucTI           = eTI_M_FT_NA_1_FaultEventInfo;//��������		
		VSQ_VarStructureQualify VSQ;        
		VSQ.ucSQ                      = 1;
		VSQ.ucNumber                  = (uint8_t)byteOfDataSizeInPack/4; //ÿ��������4�ֽڴ洢������Ҫ��Ϊ3�ֽ�
		memcpy(&(pFrameVar->FVH.ucVSQ), &VSQ, sizeof(char));
		
		pFrameVar->FVH.u16COT         = eCOT_RecordData;//����ԭ�� 
		 
		pFrameVar->FVH.u16ASDUAddr    = u16LinkAddress;
		pFrameVar->u16InfoOBJAddress  = m_nRECWaveAddr;
		
		bool bSQ = (VSQ.ucSQ == 1) ? TRUE : FALSE;
		if (bSQ)
		{
			unsigned char* pucBuf = pSendFrame->ucData + sizeof(RECORD_WAVE_HEAD);	
			
			for (int i = 0; i < byteOfDataSizeInPack ; i++)
			{
				/*ȡ���ݣ��������֡*/
				//������дģ������
				*((uint8_t *)pucBuf) = i;
				pucBuf += sizeof(uint8_t);

			}

			//����У����
			*pucBuf           = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
			pucBuf++;			
			*pucBuf           = 0x16;//��������־			
			int nLength       = pucBuf - pSendFrame->ucData + 1;
			pSendFrame->nSize = nLength; //���¼��֡���ܳ���
		}  
	}

	/*��װ�쳣����¼������֡,һ���ϴ�13�����ڲ������ݣ���Ҫ93�����Ĵ������*/
void MakePackRecordDataForFault(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint16_t curPos, uint8_t byteOfDataSizeInPack)
{

		    /*ȷ���ײ���Ϣ������*/
		RECORD_WAVE_HEAD* pFrameVar = (RECORD_WAVE_HEAD*)pSendFrame->ucData;
		pFrameVar->FVH.ucStartChar1 = 0x68;
		pFrameVar->FVH.ucLen1       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		pFrameVar->FVH.ucLen2       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		   /*�Ƿ������һ֡����������־*/
	  if (isLastDataFrame)
		{
			pFrameVar->ucEndFlag  = 1;
		}
		else{
			pFrameVar->ucEndFlag  = 0;
		}

		pFrameVar->FVH.ucStartChar2   = 0x68;		                  
		unsigned char ucLinkControl   = MakeLinkControl(TRUE, TRUE, TRUE, TRUE, eFC_Send_Confirm_UserData);//���������  
		pFrameVar->FVH.ucLinkControl  = ucLinkControl;
		pFrameVar->FVH.u16LinkAddress = u16LinkAddress;		           //��·��ַ		
		pFrameVar->FVH.ucTI           = eTI_M_FT_NA_1_FaultEventInfo;//��������		
		VSQ_VarStructureQualify VSQ;        
		VSQ.ucSQ                      = 1;
		VSQ.ucNumber                  = (uint8_t)byteOfDataSizeInPack/24; //ÿ��������4�ֽڴ洢������Ҫ��Ϊ3�ֽ�
		memcpy(&(pFrameVar->FVH.ucVSQ), &VSQ, sizeof(char));
		
		pFrameVar->FVH.u16COT         = eCOT_RecordData;//����ԭ�� 
		 
		pFrameVar->FVH.u16ASDUAddr    = u16LinkAddress;
		pFrameVar->u16InfoOBJAddress  = m_nRECWaveAddr;
		
		bool bSQ = (VSQ.ucSQ == 1) ? TRUE : FALSE;
		if (bSQ)
		{
			unsigned char* pucBuf = pSendFrame->ucData + sizeof(RECORD_WAVE_HEAD);	
			
			//memcpy(pucBuf,&(buf_fault_block.electrical_data[curPos]),byteOfDataSizeInPack);
			
			pucBuf += byteOfDataSizeInPack;//�ƶ�֡��λ��ָ��			

			//����У����
			*pucBuf           = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
			pucBuf++;			
			*pucBuf           = 0x16;//��������־			
			int nLength       = pucBuf - pSendFrame->ucData + 1;
			pSendFrame->nSize = nLength; //���¼��֡���ܳ���
		}  
	}	
	
	/*��װʵʱ¼������֡for Test*/
void MakePackForRecordData_(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint8_t byteOfDataSizeInPack)
{

		    /*ȷ���ײ���Ϣ������*/
		RECORD_WAVE_HEAD* pFrameVar = (RECORD_WAVE_HEAD*)pSendFrame->ucData;
		pFrameVar->FVH.ucStartChar1 = 0x68;
		pFrameVar->FVH.ucLen1       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		pFrameVar->FVH.ucLen2       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		   /*�Ƿ������һ֡����������־*/
	  if (isLastDataFrame)
		{
			pFrameVar->ucEndFlag  = 1;
		}
		else{
			pFrameVar->ucEndFlag  = 0;
		}

		pFrameVar->FVH.ucStartChar2   = 0x68;		                  
		unsigned char ucLinkControl   = MakeLinkControl(TRUE, TRUE, TRUE, TRUE, eFC_Send_Confirm_UserData);//���������  
		pFrameVar->FVH.ucLinkControl  = ucLinkControl;
		pFrameVar->FVH.u16LinkAddress = u16LinkAddress;		           //��·��ַ		
		pFrameVar->FVH.ucTI           = eTI_M_FT_NA_1_FaultEventInfo;//��������		
		VSQ_VarStructureQualify VSQ;        
		VSQ.ucSQ                      = 1;
		VSQ.ucNumber                  = (uint8_t)byteOfDataSizeInPack/24; //ÿ��������4�ֽڴ洢������Ҫ��Ϊ3�ֽ�
		memcpy(&(pFrameVar->FVH.ucVSQ), &VSQ, sizeof(char));
		
		pFrameVar->FVH.u16COT         = eCOT_RecordData;//����ԭ�� 
		 
		pFrameVar->FVH.u16ASDUAddr    = u16LinkAddress;
		pFrameVar->u16InfoOBJAddress  = m_nRECWaveAddr;
		
		bool bSQ = (VSQ.ucSQ == 1) ? TRUE : FALSE;
		if (bSQ)
		{
			unsigned char* pucBuf = pSendFrame->ucData + sizeof(RECORD_WAVE_HEAD);	
			/*��ʼ��λ��ָ�룬ָ�����¼��λ�ÿ�ʼ��*/
			//SAMPLE_ONE_SECOND_TypeDef *PointerOfSecondDataBlock = &(sampleCircleSequence.sampleQueue[sampleCircleSequence.Readpoint]);
			
			/*�����ݿ�ķֽ�㣬һ�����ݿ�����һ������֡*/
            /*if(sampleCircleSequence.indexInEachSecondForDataTrans+9 < REALSAMPLEPERSECOND)
			{				
				memcpy(pucBuf,&(PointerOfSecondDataBlock->electrical_data[sampleCircleSequence.indexInEachSecondForDataTrans]),sizeof(ADCHANNELS_TypeDef)*9);			
			    pucBuf += sizeof(ADCHANNELS_TypeDef)*9;//�ƶ�֡��λ��ָ			  
			    sampleCircleSequence.indexInEachSecondForDataTrans = (sampleCircleSequence.indexInEachSecondForDataTrans+9)%REALSAMPLEPERSECOND;//�ƶ�ѭ�����������ݿ�ָ��
			}				
			else
			{
				uint16_t LastSampleCountRemain = REALSAMPLEPERSECOND - sampleCircleSequence.indexInEachSecondForDataTrans;
				memcpy(pucBuf,&(PointerOfSecondDataBlock->electrical_data[sampleCircleSequence.indexInEachSecondForDataTrans]),sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain);			
			    pucBuf += sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain;//�ƶ�֡��λ��ָ			  
			    sampleCircleSequence.indexInEachSecondForDataTrans = 0;//�ƶ�ѭ�����������ݿ�ָ��, point to the first element of next second
			  
				sampleCircleSequence.Readpoint=(sampleCircleSequence.Readpoint+1)%BUFFERSIZEINSECONDS;
				
				pFrameVar->FVH.ucLen1  = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain + 2 - 6);
		        pFrameVar->FVH.ucLen2  = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain + 2 - 6);
				VSQ.ucNumber           = (uint8_t)LastSampleCountRemain;
				pFrameVar->ucEndFlag   = 1;
			}
			*/

			//����У����
			*pucBuf           = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
			pucBuf++;			
			*pucBuf           = 0x16;//��������־			
			int nLength       = pucBuf - pSendFrame->ucData + 1;
			pSendFrame->nSize = nLength; //���¼��֡���ܳ���
		}  
	}

	/*��װʵʱ¼������֡V2*/
void MakePackForRealtimeRecordData(DataPack_Type *pSendFrame,bool isLastDataFrame, uint16_t u16LinkAddress, uint8_t byteOfDataSizeInPack)
{

		    /*ȷ���ײ���Ϣ������*/
		RECORD_WAVE_HEAD* pFrameVar = (RECORD_WAVE_HEAD*)pSendFrame->ucData;
		pFrameVar->FVH.ucStartChar1 = 0x68;
		pFrameVar->FVH.ucLen1       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		pFrameVar->FVH.ucLen2       = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + byteOfDataSizeInPack + 2 - 6);
		   /*�Ƿ������һ֡����������־*/
	    if (isLastDataFrame)
		{
			pFrameVar->ucEndFlag  = 1;
		}
		else{
			pFrameVar->ucEndFlag  = 0;
		}

		pFrameVar->FVH.ucStartChar2   = 0x68;		                  
		unsigned char ucLinkControl   = MakeLinkControl(TRUE, TRUE, TRUE, TRUE, eFC_Send_Confirm_UserData);//���������  
		pFrameVar->FVH.ucLinkControl  = ucLinkControl;
		pFrameVar->FVH.u16LinkAddress = u16LinkAddress;		           //��·��ַ		
		pFrameVar->FVH.ucTI           = eTI_M_FT_NA_1_FaultEventInfo;//��������		
		VSQ_VarStructureQualify VSQ;        
		VSQ.ucSQ                      = 1;
		VSQ.ucNumber                  = (uint8_t)byteOfDataSizeInPack/24; 
		memcpy(&(pFrameVar->FVH.ucVSQ), &VSQ, sizeof(char));
		
		pFrameVar->FVH.u16COT         = eCOT_RecordData;//����ԭ�� 
		 
		pFrameVar->FVH.u16ASDUAddr    = u16LinkAddress;
		pFrameVar->u16InfoOBJAddress  = m_nRECWaveAddr;
		
		bool bSQ = (VSQ.ucSQ == 1) ? TRUE : FALSE;
		if (bSQ)
		{
			unsigned char* pucBuf = pSendFrame->ucData + sizeof(RECORD_WAVE_HEAD);	
			
			/*if(currPOs.currentSecond==99 && currPOs.currentPosInSecond+SAMPLE_COUNT_PER_RECORD_WAVE_DATA_FRAME>REALSAMPLEPERSECOND)
			{
				uint16_t LastSampleCountRemain = REALSAMPLEPERSECOND - currPOs.currentPosInSecond;
				memcpy(pucBuf,ptrCurrPos,sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain);			
			  pucBuf += sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain;//�ƶ�֡��λ��ָ			  
			  currPOs.currentPosInSecond = 0;//�ƶ�ѭ�����������ݿ�ָ��
			  
				currPOs.currentSecond =0;
				
				pFrameVar->FVH.ucLen1  = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain + 2 - 6);
		    pFrameVar->FVH.ucLen2  = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + sizeof(ADCHANNELS_TypeDef)*LastSampleCountRemain + 2 - 6);
				VSQ.ucNumber           = (uint8_t)LastSampleCountRemain;
				pFrameVar->ucEndFlag   = 1;
				
			}else
			{
				memcpy(pucBuf,ptrCurrPos,sizeof(ADCHANNELS_TypeDef)*9);			
			  pucBuf += sizeof(ADCHANNELS_TypeDef)*9;//�ƶ�֡��λ��ָ
				
			  pFrameVar->FVH.ucLen1  = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + sizeof(ADCHANNELS_TypeDef)*9 + 2 - 6);
		    pFrameVar->FVH.ucLen2  = (unsigned char)(sizeof(RECORD_WAVE_HEAD) + sizeof(ADCHANNELS_TypeDef)*9 + 2 - 6);
				VSQ.ucNumber           = 9;
				pFrameVar->ucEndFlag   = 0;				
				
        currPOs.currentSecond      =	currPOs.currentSecond + floor((currPOs.currentPosInSecond+9)/REALSAMPLEPERSECOND);		  
			  currPOs.currentPosInSecond = (currPOs.currentPosInSecond+9)%REALSAMPLEPERSECOND;//�ƶ�ѭ�����������ݿ�ָ��
				
			}
			
			ptrCurrPos = &(sampleCircleSequence.sampleQueue[currPOs.currentSecond].electrical_data[currPOs.currentPosInSecond]);			
            */
			//����У����
			*pucBuf           = CacuCheckCS(&pFrameVar->FVH.ucLinkControl, pFrameVar->FVH.ucLen1);
			pucBuf++;			
			*pucBuf           = 0x16;//��������־			
			int nLength       = pucBuf - pSendFrame->ucData + 1;
			pSendFrame->nSize = nLength; //���¼��֡���ܳ���
		}  
	}
	
//��վ-�����·����
unsigned char MakeLinkControl(bool bDIR,bool bPRM,bool bFCB,bool bFCV,unsigned char ucCode)
{
	unsigned char ucLinkControl = 0;
	if(bDIR)
		ucLinkControl |= 0x80;
	if(bPRM)
		ucLinkControl |= 0x40;
	if(bFCB)
		ucLinkControl |= 0x20;
	if(bFCV)
		ucLinkControl |= 0x10;

	ucCode &= 0x0F;
	ucLinkControl = ucLinkControl|ucCode;
	return ucLinkControl;
}



bool IsFrameFix(unsigned char* pBuffer,int nBufferSize)
{
	if(IEC101_FRAME_FIX_SIZE!=nBufferSize||NULL==pBuffer)
		return FALSE;
	FIXEDFRAME* pFrameFix = (FIXEDFRAME*)pBuffer;
	if(0x10==pFrameFix->ucStartChar&&0x16==pFrameFix->ucEndChar)
	{
		unsigned char* pucAddress  = (unsigned char*)&pFrameFix->u16LinkAddress;
		unsigned char  ucCheckCode = (pFrameFix->ucLinkControl + pucAddress[0] + pucAddress[1])%256;
		if(ucCheckCode==pFrameFix->ucCheckCode)
			return TRUE;
	}
	return FALSE;
}

bool IsFrameVar(unsigned char* pBuffer,int nBufferSize,int* nRealPackSize)
{
	(*nRealPackSize) = -1;
	if(sizeof(FRAME_VAR_HEAD)>nBufferSize||NULL==pBuffer)
		return FALSE;
	FRAME_VAR_HEAD* pFrameVar = (FRAME_VAR_HEAD*)pBuffer;

	//�ȶ������ַ�������0x68
	if(0x68==pFrameVar->ucStartChar1&&0x68==pFrameVar->ucStartChar2)
	{
		//����1�ͳ���2�������
		if(pFrameVar->ucLen1==pFrameVar->ucLen2)
		{
			//���������������ȱ�����ڵ��ڼ�������ĳ���
			int nDataLength = pFrameVar->ucLen1;
			int nPackSize = nDataLength + 6;
			if(nBufferSize<nPackSize)
				return FALSE;
			//�����ַ�������0x16
			if(0x16!=pBuffer[6+nDataLength-1])
				return FALSE;
			//����ʹӿ�����ʼ����
			unsigned char* pucData = &pFrameVar->ucLinkControl;
			unsigned int  uCheckCode = 0;
			for(int i=0;i<nDataLength;i++)
			{
				uCheckCode = uCheckCode + pucData[i];
			}
			uCheckCode = uCheckCode%256;
			//У��ͶԾͽ���ɹ�
			if(uCheckCode==pBuffer[6+nDataLength-2])
			{
				(*nRealPackSize) = nPackSize;
				return TRUE;
			}
		}
	}
	return FALSE;
}

//����COT
void ParseCOT(uint16_t u16COT,COT_Detail* CTD)
{
	CTD->ucOriginatorAddress = (u16COT&0xFF00)>>8;
	CTD->bPN   = u16COT&0x0040 ? TRUE:FALSE;
	CTD->bTest = u16COT&0x0080 ? TRUE:FALSE;
	unsigned char ucCOT = (unsigned char)(u16COT&0x003F);
	switch ((enumCOT)ucCOT)
	{
			case eCOT_NoUse:
			case eCOT_per_cyc:
			case eCOT_back_scan:
			case eCOT_spont:           
			case eCOT_init:               
			case eCOT_req:        
			case eCOT_act:                  
			case eCOT_actcon:           
			case eCOT_deact:            
			case eCOT_deactcon:     
			case eCOT_actterm:          
			case eCOT_fileTrans:             
			case eCOT_introgen:       
			case eCOT_reqcogen: 
			case eCOT_unknownTID:     
			case eCOT_unknownCOT:            
			case eCOT_unknownASPUcommAddr:
			case eCOT_unknownInfoObjAddr:        
			case eCOT_RomoteControlStatusErr:    
			case eCOT_RemoteControlTimeStampErr:        
			case eCOT_RemoteControlDigitalSignAuthenErr:
      case eCOT_RecordData:
      case eCOT_RemoteControlConfig:				
				 CTD->eCOT = (enumCOT)ucCOT;
                 break;
			default:
				CTD->eCOT = eCOT_UnDefined;
				break;
	}
}

bool ParseMeasure(unsigned char* pBuffer,int nBufferSize,MeasureContentLink* Array)
{
	MeasureContentLink* tmpLink = NULL;
	MeasureContentLink* switchLink = NULL;
	if(NULL==pBuffer||nBufferSize<=sizeof(FRAME_VAR_HEAD))
		return FALSE;

	FRAME_VAR_HEAD*  pFVH = (FRAME_VAR_HEAD*)pBuffer;
	int nDataLength = pFVH->ucLen1;
	if(nBufferSize!=nDataLength+6)
		return FALSE;

	VSQ_VarStructureQualify VSQ;
	memcpy(&VSQ,&pFVH->ucVSQ,1);
	//�����׵�ַ
	unsigned char* pucDataStart = pBuffer + sizeof(FRAME_VAR_HEAD);
	enum_TI_TYPEID eTi = (enum_TI_TYPEID)(pFVH->ucTI);
	if(0==VSQ.ucSQ)
	{
		unsigned char* pucData = pucDataStart;
		for(unsigned char i=0;i<VSQ.ucNumber;i++)
		{
			MeasureContent stMC = {0};
			//---------------------------------------------------------------------------
			stMC.u16RemoteMeasureObjAddr = *(uint16_t*)pucData; 
			pucData +=2;
			//---------------------------------------------------------------------------
			switch (eTi)
			{
			case eTI_M_ME_NA_1_Measure_Normalized:
			case eTI_M_ME_NB_1_Measure_Scaled :
				stMC.fMeasure = *(uint16_t*)(pucData);
				pucData +=2;
				//��һ��ֵת��
				break;
			case eTI_M_ME_NC_1_Measure_ShortFloatValue:
				stMC.fMeasure = *(float*)(pucData);
				pucData +=4;
				break;
			default:
				break;
			
			}
			stMC.ucQDS = *(pucData);
			pucData++;
			//---------------------------------------------------------------------------
			//�����ýṹ��
			MeasureContent* mcPoint = malloc(sizeof(MeasureContent));
			memcpy(mcPoint,&stMC,sizeof(MeasureContent));
			MeasureContentLink* mcLinkPoint = malloc(sizeof(MeasureContentLink));
			mcLinkPoint->measureCon = mcPoint;
			mcLinkPoint->next = NULL;
			tmpLink = Array;
			while(tmpLink->next != NULL)
			{
				tmpLink = tmpLink->next;
			}
			tmpLink->next = mcLinkPoint;			
		}
	}
	else
	{
		//ң�Ŷ���1�ĵ�ַ
		uint16_t u16Address1 = *(uint16_t*)pucDataStart; 
		unsigned char* pucData  = pucDataStart + sizeof(uint16_t);
		for(unsigned char i=0;i<VSQ.ucNumber;i++)
		{
			MeasureContent stMC = {0};
			//---------------------------------------------------------------------------
			stMC.u16RemoteMeasureObjAddr = u16Address1+i; 
			//---------------------------------------------------------------------------
			switch (eTi)
			{
			case eTI_M_ME_NA_1_Measure_Normalized:
			case eTI_M_ME_NB_1_Measure_Scaled:
				stMC.fMeasure = *(uint16_t*)(pucData);
				pucData +=2;
				//��һ��ֵת��...
				break;
			case eTI_M_ME_NC_1_Measure_ShortFloatValue:
				stMC.fMeasure = *(float*)(pucData);
				pucData +=4;
				break;
			default:
				break;
			}
			stMC.ucQDS = *(pucData);
			pucData++;
			//---------------------------------------------------------------------------
			//�����ýṹ��
			MeasureContent* mcPoint = malloc(sizeof(MeasureContent));
			memcpy(mcPoint,&stMC,sizeof(MeasureContent));
			MeasureContentLink* mcLinkPoint = malloc(sizeof(MeasureContentLink));
			mcLinkPoint->measureCon = mcPoint;
			mcLinkPoint->next = NULL;
			tmpLink = Array;
			while(tmpLink->next != NULL)
			{
				tmpLink = tmpLink->next;
			}
			tmpLink->next = mcLinkPoint;		
		}
	}
	return TRUE;
}

bool ParseSignal(unsigned char* pBuffer,int nBufferSize,SignalContentLink_Type* Array)
{
	SignalContentLink_Type* tmpLink = NULL;
	SignalContentLink_Type* switchLink = NULL;
	if(NULL==pBuffer||nBufferSize<=sizeof(FRAME_VAR_HEAD))
		return FALSE;

	FRAME_VAR_HEAD*  pFVH = (FRAME_VAR_HEAD*)pBuffer;
	int nDataLength = pFVH->ucLen1;
	if(nBufferSize!=nDataLength+6)
		return FALSE;

	VSQ_VarStructureQualify VSQ;
	memcpy(&VSQ,&pFVH->ucVSQ,1);
	//�����׵�ַ
	unsigned char* pucDataStart = pBuffer + sizeof(FRAME_VAR_HEAD);
	enum_TI_TYPEID eTi = (enum_TI_TYPEID)(pFVH->ucTI);

	bool bHaveCP56Time2a = FALSE;
	switch (eTi)
	{
	case eTI_M_SP_NA_1_SingleInfo:
	case eTI_M_DP_NA_1_DoubleInfo:
		break;
	case eTI_M_SP_TB_1_CP56Time2aBasedSingleInfo:
	case eTI_M_DP_TB_1_CP56Time2aBasedDoubleInfo:
		bHaveCP56Time2a = TRUE;
		break;
	default:
		assert(FALSE);
	}
	if(0==VSQ.ucSQ)
	{
		unsigned char* pucData = pucDataStart;
		for(unsigned char i=0;i<VSQ.ucNumber;i++)
		{
			SignalContent_Type stSC = {0};
			//---------------------------------------------------------------------------
			stSC.u16RemoteSignalObjAddr = *(uint16_t*)pucData; 
			pucData +=2;
			//---------------------------------------------------------------------------
			stSC.ucSignal = *(pucData);
			pucData++;
			//---------------------------------------------------------------------------
			if(bHaveCP56Time2a)
			{
				memcpy(&stSC.st,pucData,sizeof(CP56Time2a));
				pucData+=7;
			}
			stSC.bHaveCP56Time2a = bHaveCP56Time2a;
			//---------------------------------------------------------------------------
			//Array.push_back(stSC);
			//�����ýṹ��
			SignalContent_Type* scPoint = malloc(sizeof(SignalContent_Type));
			memcpy(scPoint,&stSC,sizeof(SignalContent_Type));
			SignalContentLink_Type* scLinkPoint = malloc(sizeof(SignalContentLink_Type));
			scLinkPoint->signalCon = scPoint;
			scLinkPoint->next = NULL;
			tmpLink = Array;
			while(tmpLink->next != NULL)
			{
				tmpLink = tmpLink->next;
			}
			tmpLink->next = scLinkPoint;		
			
		}
	}
	else
	{
		//ң�Ŷ���1�ĵ�ַ
		uint16_t u16Address1 = *(uint16_t*)pucDataStart; 
		unsigned char* pucData = pucDataStart + sizeof(uint16_t);
		for(unsigned char i=0;i<VSQ.ucNumber;i++)
		{
			SignalContent_Type stSC = {0};
			//---------------------------------------------------------------------------
			stSC.u16RemoteSignalObjAddr = u16Address1+i; 
			//---------------------------------------------------------------------------
			stSC.ucSignal = *(pucData);
			pucData++;
			//---------------------------------------------------------------------------
			if(bHaveCP56Time2a)
			{
				memcpy(&stSC.st,pucData,sizeof(CP56Time2a));
				pucData+=7;
			}
			stSC.bHaveCP56Time2a = bHaveCP56Time2a;
			//---------------------------------------------------------------------------
			//Array.push_back(stSC);
			//�����ýṹ��
			SignalContent_Type* scPoint = malloc(sizeof(SignalContent_Type));
			memcpy(scPoint,&stSC,sizeof(SignalContent_Type));
			SignalContentLink_Type * scLinkPoint = malloc(sizeof(SignalContentLink_Type));
			scLinkPoint->signalCon = scPoint;
			scLinkPoint->next = NULL;
			tmpLink = Array;
			while(tmpLink->next != NULL)
			{
				tmpLink = tmpLink->next;
			}
			tmpLink->next = scLinkPoint;
		}
	}
	return TRUE;
}

 TimeStruct ParseCP56Time2a(CP56Time2a stTime)
{
		TimeStruct t;
		unsigned ucSeconds = stTime.u16Milliseconds/1000;
		if(ucSeconds>60)
		ucSeconds = 59;
		t.year = stTime.ucYears;
		t.month = stTime.ucMonths;
		t.day = stTime.ucDayOfMonth;
		t.week = stTime.ucDayOfWeek;
		t.hour = stTime.ucHours;
		t.min = stTime.ucMinutes;
		t.sec = ucSeconds;
		return t;
}

 CP56Time2a MakeCP56Time2a(TimeStruct t)
{
		CP56Time2a stTime      = {0};
		stTime.ucYears         = t.year;
		stTime.ucMonths        = t.month;
		stTime.ucDayOfMonth    = t.day;
		stTime.ucDayOfWeek     = t.week;
		stTime.ucHours         = t.hour;
		stTime.ucMinutes       = t.min;
		stTime.u16Milliseconds = t.sec*1000;
		return stTime;
}

CP56Time2a MakeCP56Time2aFromCP56(CP56Time2a *t)
{
		CP56Time2a stTime      = {0};
		stTime.ucYears         = t->ucYears;
		stTime.ucMonths        = t->ucMonths;
		stTime.ucDayOfMonth    = t->ucDayOfMonth;
		stTime.ucDayOfWeek     = t->ucDayOfWeek;
		stTime.ucHours         = t->ucHours;
		stTime.ucMinutes       = t->ucMinutes;
		stTime.u16Milliseconds = t->u16Milliseconds;
		return stTime;
}


uint32_t GetDifferenceTime(TimeStruct * curr, TimeStruct *old)
{
	//�˴�����Ҫ��һ������
	
	uint32_t totalSecs = 0;
//	if(curr->sec >= old->sec)
//		totalSecs = curr->sec - old->sec;
//	else
//	{
//		//curr->min = curr->min -1;
//		curr->sec = curr->sec + 60;
//		totalSecs = curr->sec - old->sec;
//	}
	
	if(curr->min>=old->min)
	{
		totalSecs=abs((curr->min*60+curr->sec)-abs(old->min*60+old->sec));
	}else
	{
		totalSecs = (curr->min-old->min+60)*60+abs(curr->sec-old->sec);
	}
	
	return totalSecs;
	
}

void  RemoteMeasureUpdate(void)
{
	//��Ӧ��Ϊһ�����壬һ���޸�
	//memcpy(remoteMeasure.data,powersingledata,160);
	MakePackForRemoteMeasure(&g_RemoteMeasureFrame,&remoteMeasure,m_u16LinkAddress);
	g_pRemoteMeasureAll.data = &g_RemoteMeasureFrame;
	g_pRemoteMeasureAll.next = NULL;
	g_pRemoteMeasureAll.nTotalCount = 1;
	g_pRemoteMeasureAll.nSendedIndex = -1;	
}


